package cn.felord.common.excel;

import com.alibaba.excel.EasyExcel;
import jakarta.validation.Validator;
import lombok.AllArgsConstructor;


/**
 * excel读取工具
 *
 * @author felord.cn
 * @since 2021 /4/14 15:10
 */
@AllArgsConstructor
public class ValidatedExcelReader {
    private final Validator validator;

    /**
     * Read Excel.
     *
     * @param <T>  the type parameter
     * @param meta the meta
     */
    public <T> void read(ExcelMeta<T> meta) {
        ExcelValidator<T> excelValidator = new ExcelValidator<>(validator, meta.getHeadRowNumber());
        JdbcEventListener<T> readListener = new JdbcEventListener<>(excelValidator, meta.getConsumer());
        EasyExcel.read(meta.getExcelStream(), meta.getDomain(), readListener)
                .headRowNumber(meta.getHeadRowNumber())
                .sheet()
                .doRead();
    }
}
